<?php


namespace app\adminapi\lists\user;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExtendInterface;
use app\common\lists\ListsSearchInterface;
use app\common\model\user\User;

/**
 * @projectName src
 */
class UserLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
{

    /**
     * 设置搜索条件
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['user_name', 'user_phone'],
            '=' => ['id', 'user_gender', 'user_idno']
        ];
    }

    public function queryWhere()
    {
        $where = [];
        if (!empty($this->params['user_type_name'])) {
            $where[] = ['ut.type_name', 'like', '%' . $this->params['user_type_name'] . '%'];
        }
        return $where;
    }

    /**
     * 获取列表
     * @return array
     */
    public function lists(): array
    {
        $userLists = (new User())
            ->field('h_user.*,ut.type_name')
            ->join('h_user_type ut', 'h_user.user_type = ut.id')
            ->order(['h_user.id' => 'desc'])
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();
        return $userLists;
    }

    /**
     * 获取数量
     */
    public function count(): int
    {
        return (new User())
            ->join('h_user_type ut', 'h_user.user_type = ut.id')
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->count();
    }

    /**
     * 额外参数
     * extend
     * @return array
     */
    public function extend()
    {
        return [];
    }

}